package com.transcasafe.eanpay.core.cipher;

import com.transcasafe.eanpay.core.auth.Credential;
import com.transcasafe.eanpay.core.http.HttpHeaders;
import com.transcasafe.eanpay.core.http.PayConstant;
import org.apache.commons.lang3.StringUtils;

/**
 * 验签器
 */
public final class EanPayVerifier implements Verifier {

    private final Credential credential;

    public EanPayVerifier(Credential credential) {
        this.credential = credential;
    }

    @Override
    public boolean verify(HttpHeaders headers, String message) {
        // 获取签名头
        String signatureHeader = headers.getHeader(PayConstant.HTTP_HEADER_SIGNATURE);
        // 获取签名内容
        String signature = StringUtils.substringAfter(signatureHeader, "signature=");
        // 验签
        return StringUtils.equalsAnyIgnoreCase(credential.getAuthorization(headers, message), signature);
    }
}
